package online.foxnull2.foxmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import online.foxnull2.foxmall.coupon.entity.SeckillSkuRelationEntity;
import online.foxnull2.foxmall.coupon.mapper.SeckillSkuRelationMapper;
import online.foxnull2.foxmall.coupon.service.SeckillSkuRelationService;
import online.foxnull2.foxmall.coupon.utils.PageUtils;
import online.foxnull2.foxmall.coupon.utils.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Slf4j
@Service("seckillSkuRelationService")
public class SeckillSkuRelationServiceImpl extends ServiceImpl<SeckillSkuRelationMapper, SeckillSkuRelationEntity> implements SeckillSkuRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SeckillSkuRelationEntity> queryWrapper = new QueryWrapper<>();
        Object promotionSessionId = params.get("promotionSessionId");
        if (!ObjectUtils.isEmpty(promotionSessionId)) {
            queryWrapper.eq("promotion_session_id", promotionSessionId);
        }
        IPage<SeckillSkuRelationEntity> page = this.page(
                new Query<SeckillSkuRelationEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSkuRelationEntity> listSeckillSkuRelationByPromotionSessionId(Long promotionSessionId) {
        List<SeckillSkuRelationEntity> seckillSkuRelationEntities = baseMapper.selectList(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", promotionSessionId));
        log.debug("listSeckillSkuRelationByPromotionSessionId -> {}", seckillSkuRelationEntities);
        return seckillSkuRelationEntities;
    }

}